package cst.neu.yuejian_31.model;

import java.util.List;

import cst.neu.yuejian_31.bean.ActivityBean;
import cst.neu.yuejian_31.iface.ActivityDetailIface;
import cst.neu.yuejian_31.iface.ActivityDetailListener;
import cst.neu.yuejian_31.iface.EventIface;
import cst.neu.yuejian_31.iface.EventListener;
import cst.neu.yuejian_31.service.ActivityDetailService;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.EventService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Alienware on 2017/10/15.
 */

public class ActivityModel implements EventIface,ActivityDetailIface {
    private Retrofit retrofit;
    //    private String BASEURL="http://10.0.2.2/neu16_yuejian/api/Club/";
    public ActivityModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getActivityList(final EventListener EventListener) {
        EventService EventService=retrofit.create(EventService.class);
        Call<List<ActivityBean>> call
                =EventService.getActivitylist();
        call.enqueue(new Callback<List<ActivityBean>>() {
            @Override
            public void onResponse(Call<List<ActivityBean>> call, Response<List<ActivityBean>> response) {
                if (response.isSuccessful() && response.body() != null)
                {
                    EventListener.onResponse(response.body());
                }
                else {
                    EventListener.onFail("FAIL");

                }
            }

            @Override
            public void onFailure(Call<List<ActivityBean>> call, Throwable t) {

                EventListener.onFail(t.toString());
            }
        });
    }
    @Override
    public void getactivitydetail(String activity_id,final ActivityDetailListener ActivityDetailListener) {
        ActivityDetailService ActivityDetailService=retrofit.create(ActivityDetailService.class);
        Call<ActivityBean>call
                =ActivityDetailService.getactivitydetail(activity_id);
        call.enqueue(new Callback<ActivityBean>() {
            @Override
            public void onResponse(Call<ActivityBean> call, Response<ActivityBean> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    ActivityDetailListener.onResponse(response.body());
                }
                else {
                    ActivityDetailListener.onFail("FAIL");
                }
            }

            @Override
            public void onFailure(Call<ActivityBean> call, Throwable t) {

                ActivityDetailListener.onFail(t.toString());
            }
        });
    }
}